package com.jvyou.app.ds.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jvyou.app.common.constants.BaseConstants;
import com.jvyou.app.common.domain.dto.DictDataDto;
import com.jvyou.app.common.domain.entity.DictData;
import com.jvyou.app.common.domain.model.PageResult;
import com.jvyou.app.common.domain.model.Result;
import com.jvyou.app.common.enums.HttpCodeEnum;
import com.jvyou.app.common.utils.orm.WrapperTool;
import com.jvyou.app.ds.mapper.DictDataMapper;
import com.jvyou.app.ds.service.DictDataService;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.util.List;


/**
 * 字典数据表(DictData)表服务实现类
 *
 * @author 橘柚
 * @since 2025-03-13 09:50:54
 */
@Service("dictDataService")
public class DictDataServiceImpl extends ServiceImpl<DictDataMapper, DictData> implements DictDataService {

    private QueryWrapper<DictData> getWrapper(DictDataDto dictDataDto) {
        QueryWrapper<DictData> queryWrapper = new QueryWrapper<>();
        WrapperTool.notEmptyEq(queryWrapper, "dict_label", dictDataDto.getDictLabel());
        WrapperTool.notEmptyEq(queryWrapper, "dict_type", dictDataDto.getDictType());
        queryWrapper.orderByAsc("dict_sort");
        return queryWrapper;
    }

    @Override
    public PageResult<DictData> pageDictData(DictDataDto dictDataDto) {
        Page<DictData> page = new Page<>(dictDataDto.getCurrent(), dictDataDto.getPageSize());
        return PageResult.success(page(page, getWrapper(dictDataDto)));
    }

    @Override
    public List<DictData> listDictData(DictDataDto dictDataDto) {
        return list(getWrapper(dictDataDto));
    }

    @Override
    public Result<DictData> insertDictData(DictData dictData) {
        // 1. 首先判断同等类型下面存不存在默认值，保证默认值能能有一个
        if (dictData.getIsDefault().equals(BaseConstants.YES)) {
            LambdaQueryWrapper<DictData> dictDataWrapper = new LambdaQueryWrapper<>();
            dictDataWrapper.eq(DictData::getDictType, dictData.getDictType());
            dictDataWrapper.eq(DictData::getIsDefault, BaseConstants.YES);
            long count = count(dictDataWrapper);
            if (count > 0) {
                return Result.error(HttpCodeEnum.DEFAULT_VALUE_EXIST);
            }
        }
        // 2. 插入数据
        return save(dictData) ? Result.success(dictData) : Result.error(HttpCodeEnum.ADD_FAIL);
    }

    @Cacheable(cacheNames = "dictData", key = "#dictTypeId + ':' + #dictValue")
    @Override
    public String getLabel(Serializable dictTypeId, String dictValue) {
        // 1. 类型ID 为空，返回空
        if (dictTypeId == null) {
            return "";
        }
        // 2. 字典值为空尝试查询默认值
        if (dictValue == null) {
            LambdaQueryWrapper<DictData> dictDataWrapper = new LambdaQueryWrapper<>();
            dictDataWrapper.eq(DictData::getDictType, dictTypeId);
            dictDataWrapper.eq(DictData::getIsDefault, BaseConstants.YES);
            dictDataWrapper.last("limit 1");
            DictData dictData = getOne(dictDataWrapper);
            if (dictData != null) {
                return dictData.getDictLabel();
            }
        }
        // 3. 查询字典值对应的字典标签
        LambdaQueryWrapper<DictData> dictDataWrapper = new LambdaQueryWrapper<>();
        dictDataWrapper.eq(DictData::getDictType, dictTypeId);
        dictDataWrapper.eq(DictData::getDictValue, dictValue);
        dictDataWrapper.last("limit 1");
        DictData dictData = getOne(dictDataWrapper);
        return dictData != null ? dictData.getDictLabel() : "";
    }
}
